home *** CD-ROM | disk | FTP | other *** search
/ Leonardo the Inventor / Leonardo The Inventor (93026)(Broderbund)(Riverdeep)(2004).iso / LEOWINMV / DATABASE.DIR / 00088_Script_Search < prev    next >
Text File  |  1996-03-28  |  7KB  |  217 lines

  1. -- --------------------------------------------------------
  2. -- Handler clickedFindButton is called when the user clicks the find button.
  3.  
  4. on clickedFindButton
  5.   global findButton
  6.   
  7.   if  (isEnabled(findButton)) then
  8.     doFind line 1 of the text of cast "searchTopic"
  9.   else
  10.     setSearchMode
  11.   end if
  12. end
  13.  
  14. -- --------------------------------------------------------
  15. -- Handler doFind 
  16.  
  17. on doFind input
  18.   global findButton
  19.   
  20.   if isEnabled(findButton) then
  21.     clearDatabase
  22.     activateButtonKeepActivated(findButton)
  23.     findUserTypedTopic input
  24.     enableButton(findButton)
  25.   end if
  26. end
  27.  
  28. -- --------------------------------------------------------
  29. -- Handler findUserTypedTopic 
  30.  
  31. on findUserTypedTopic input
  32.   global browserTopics, browserTopLine, numVisibleTopics, browserScroll
  33.   global numLinesPerBrowserEntry
  34.   
  35.   if inputContainsIllegalChar(input) then
  36.     put "illegal character(s)" into field "searchTopic"
  37.     set the selstart = 0
  38.     set the selEnd = 999
  39.     put "" into field "browser"
  40.     setSearchSuccessFul(FALSE)
  41.     exit
  42.   end if
  43.   
  44.   waitCursor
  45.   set searchResults = doSearch(input)
  46.   normalCursor
  47.   
  48.   if not(voidP(searchResults)) and not (searchResults = "") and not (searchResults = RETURN)then
  49.     set browserTopics = searchResults & RETURN
  50.     set browserTopLine = 1
  51.     moveScrollSquareToMatchText
  52.     
  53.     setBrowserText
  54.     
  55.     setSearchSuccessFul(TRUE)
  56.     selectTopic(1)
  57.     showSelectedTopic
  58.   else
  59.     put "No topics found" into field "searchTopic"
  60.     set the selstart = 0
  61.     set the selEnd = 999
  62.     put "" into field "browser"
  63.     setSearchSuccessFul(FALSE)
  64.   end if
  65. end
  66.  
  67. -- --------------------------------------------------------
  68. -- Handler doSearch 
  69.  
  70. on doSearch input 
  71.   -- To speed up the search, treat the cases separately:
  72.   -- Case 1: input is empty - do nothing.
  73.   if (input = EMPTY) then exit
  74.   
  75.   -- Case 2: input has one word - get existing list
  76.   -- of topics that contain the word.
  77.   if (the number of words in input = 1) and (not(isIgnorableWord(word 1 of input))) then
  78.     set searchResults = searchOneTopic(input)
  79.     return searchResults
  80.   end if
  81.   
  82.   -- Cast 3: input has more than 1 word including ignorable words.
  83.   -- NOTE: cases 3 and 4 were separated to make the code more readable, even though
  84.   -- they could have both been contained in one IF/THEN statement.
  85.   if (the number of words in input > 1) and (containsIgnorableWords(input)) then
  86.     set newInput = removeIgnorableWords(input)
  87.     set searchResults = doSearch(newInput)
  88.     return searchResults
  89.   end if
  90.   
  91.   -- Cast 4: input has more than 1 word and no ignorable words
  92.   if (the number of words in input > 1) and not(containsIgnorableWords(input)) then
  93.     -- set containingList = the union of topics that contain any input word  
  94.     set containingList = []
  95.     
  96.     set numTopics = the number of words in input
  97.     
  98.     -- each word they type represents an individually searchable topic
  99.     -- each topic must therefore appear in numTopics lists of topics to
  100.     -- contain all the words typed. (example: they type "king david" - 
  101.     -- each topic must contain the words "king" and "david" and must
  102.     -- therefore be found in the list of all topics containing "king"
  103.     -- and the list of all topics containing "david", for a total of
  104.     -- two lists.
  105.     
  106.     repeat with i = 1 to numTopics
  107.       set topicsContainingCurrentTopic = searchOneTopic(word i of input)
  108.       repeat with j = 1 to the number of lines in topicsContainingCurrentTopic
  109.         -- add the field name to the list
  110.         add(containingList,line j of topicsContainingCurrentTopic)
  111.       end repeat
  112.     end repeat
  113.     
  114.     sort(containingList)
  115.     -- now, any topic that appears numTopics times in containingList contains
  116.     -- all the topics typed.
  117.     
  118.     -- set searchResults = the fields that contain all search words
  119.     set searchResults = ""
  120.     
  121.     repeat with i = 1 to count(containingList) - numTopics + 1
  122.       set currentTopic = getAt(containingList,i)
  123.       set containsAll = TRUE
  124.       -- if the next numTopics-1 items in containingList are also currentTopic,
  125.       -- the currentTopic appears numTopics times and therefore contains all
  126.       -- searched for topics
  127.       repeat with j = 1 to numTopics - 1
  128.         if not(getAt(containingList,i+j) = currentTopic) then
  129.           set containsAll = FALSE
  130.           exit repeat
  131.         end if
  132.       end repeat
  133.       if containsAll = TRUE then
  134.         put currentTopic & RETURN after searchResults
  135.       end if
  136.     end repeat
  137.     return searchResults
  138.   end if
  139. end
  140.  
  141. -- --------------------------------------------------------
  142. -- Handler isIgnorableWord 
  143.  
  144. on isIgnorableWord whichWord
  145.   if (whichWord = "and") or (whichWord = "of") or (whichWord = "the") or (whichWord = "a") or (whichWord = "an") or (whichWord contains "[") or (whichWord = "-") then
  146.     return TRUE
  147.   else
  148.     return FALSE
  149.   end if
  150. end
  151.  
  152. -- --------------------------------------------------------
  153. -- Handler searchOneTopic 
  154.  
  155. on searchOneTopic topic
  156.   return getIndexIndexData (topic)
  157. end
  158.  
  159. -- --------------------------------------------------------
  160. -- Handler containsIgnorableWords 
  161.  
  162. on containsIgnorableWords phrase
  163.   repeat with i = 1 to the number of words in phrase
  164.     if (isIgnorableWord(word i of phrase)) then
  165.       return TRUE
  166.     end if
  167.   end repeat
  168.   
  169.   return FALSE
  170. end
  171.  
  172. -- --------------------------------------------------------
  173. -- Handler removeIgnorableWords 
  174.  
  175. on removeIgnorableWords phrase
  176.   set newPhrase = ""
  177.   
  178.   repeat with i = 1 to the number of words in phrase
  179.     if not(isIgnorableWord(word i of phrase)) then
  180.       set newPhrase = addWordToString(word i of phrase,newPhrase)
  181.     end if
  182.   end repeat
  183.   
  184.   return newPhrase
  185. end
  186.  
  187. -- --------------------------------------------------------
  188. -- Handler addWordToString 
  189.  
  190. on addWordToString whichWord, phrase
  191.   if (phrase = EMPTY) then
  192.     return whichWord
  193.   else
  194.     return phrase && whichWord
  195.   end if
  196. end
  197.  
  198. -- --------------------------------------------------------
  199. -- Handler setSearchSuccessFul
  200.  
  201. on setSearchSuccessFul val
  202.   global searchSuccessFul
  203.   
  204.   set searchSuccessFul = val
  205. end
  206.  
  207. -- --------------------------------------------------------
  208. -- Handler inputContainsIllegalChar
  209.  
  210. on inputContainsIllegalChar input
  211.   return (input contains "[") or (input contains "]") or (input contains "{") or (input contains "}") or (input contains "(") or (input contains ")")
  212. end
  213.  
  214. on isNumber whichWord
  215.   return not(voidP(value(whichWord)))
  216. end
  217.